package com.py.korona.ui.activities.category;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import com.py.korona.R;
import com.py.korona.db.UriBuilder;
import com.py.korona.db.model.CategoryEntity;
import com.py.korona.ui.controls.activities.CustomActivity;

public class CategoryDetailActivity extends CustomActivity implements CategoryDetailFragmentListener {
    private Uri uri_;

    @Override
    public boolean isDetailStateAllowed(CustomActivityDetailState state) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onEnteringEditMode() {
        CategoryDetailFragmentEditor fragment = new CategoryDetailFragmentEditor();
        fragment.setListener(this);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.category_detail_container, fragment);
        ft.commit();
    }

    @Override
    public void onEnteringViewMode() {
        CategoryDetailFragmentViewer fragment = new CategoryDetailFragmentViewer();
        fragment.setListener(this);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.category_detail_container, fragment);
        ft.commit();
    }

    @Override
    public ContentValues fragmentContentValues(Fragment fragment) {
        ContentValues result = new ContentValues();
        if (null != uri_) {
            Cursor cursor = getContentResolver().query(uri_, new String[] {
                    CategoryEntity.FieldNameText, CategoryEntity.FieldNameID
            }, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    result.put(CategoryEntity.FieldNameText, cursor.getString(cursor.getColumnIndexOrThrow(CategoryEntity.FieldNameText)));
                    result.put(CategoryEntity.FieldNameID, cursor.getString(cursor.getColumnIndexOrThrow(CategoryEntity.FieldNameID)));
                }
                cursor.close();
            }
        }
        return result;
    }

    @Override
    public void fragmentOnCloseAction(Fragment fragment) {
        finish();
    }

    @Override
    public void fragmentOnDeleteAction(Fragment fragment) {
        if (null != uri_) {
            getContentResolver().delete(uri_, null, null);
        }
    }

    @Override
    public void fragmentOnEditAction(Fragment fragment) {
        this.setDetailState(CustomActivityDetailState.CustomActivityDetailStateEdit);
    }

    @Override
    public void fragmentOnSaveValues(Fragment fragment, ContentValues values) {
        if (null != uri_) {
            getContentResolver().update(uri_, values, null, null);
        } else {
            Uri contentUri = UriBuilder.objectListUri(CategoryEntity.uriAuthority(), CategoryEntity.uriBasePath());
            getContentResolver().insert(contentUri, values);
        }
    }

    @Override
    public void fragmentOnViewAction(Fragment fragment) {
        this.setDetailState(CustomActivityDetailState.CustomActivityDetailStateView);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_detail_container);
        /*
         * extras
         */
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            /** extract uri */
            String contentItemType = UriBuilder.contentType(CategoryEntity.uriBasePath());
            uri_ = bundle.getParcelable(contentItemType);
            /** extract readonly */
            boolean readonly = bundle.getBoolean("readonly");
            if (readonly) {
                this.setDetailState(CustomActivityDetailState.CustomActivityDetailStateView);
            } else {
                this.setDetailState(CustomActivityDetailState.CustomActivityDetailStateEdit);
            }
        } else {
            this.setDetailState(CustomActivityDetailState.CustomActivityDetailStateEdit);
        }
    }
}
